#include "Resource.h"

namespace util
{
    namespace resource
    {
        void ReadBinFileFromStaticMemory(char *buf, BinFile*& bfile, int start, int end)
        {
            end -= (start - 1);
            while(start--)
            {
                buf += *(reinterpret_cast<uintmax *>(buf)) + sizeof(uintmax);
            }
            for(int i = 0; i < end; i++){
                bfile[i].size = *reinterpret_cast<uintmax *>(buf);
                bfile[i].data = buf + sizeof(uintmax);
                buf += *(reinterpret_cast<uintmax *>(buf)) + sizeof(uintmax);
            }
        }

        void ReadBinFileFromStaticMemory(char *buf, BinFile& bfile, int index)
        {
            while(index--)
            {
                buf += *(reinterpret_cast<uintmax *>(buf)) + sizeof(uintmax);
            }
            bfile.size = *reinterpret_cast<uintmax *>(buf);
            bfile.data = buf + sizeof(uintmax);
        }

        BinFile::BinFile() : data{nullptr}, size(0){}

        BmpFile::BmpFile() :
            fileHeader(nullptr),
            info(nullptr),
            rgb(nullptr),
            data(nullptr),
            size(0)
        {

        }

        BmpFile::BmpFile(BinFile& bin){
            char * rptr = bin.data;
            this->fileHeader = reinterpret_cast<BITMAPFILEHEADER*>(rptr);
            rptr += sizeof(BITMAPFILEHEADER);
            this->info = reinterpret_cast<BITMAPINFOHEADER *>(rptr);
            rptr += sizeof(BITMAPINFOHEADER);
            this->rgb = reinterpret_cast<RGBQUAD (*)[256]>(rptr);
            rptr += sizeof(RGBQUAD) * 256;
            this->data = rptr;
            this->size = bin.size;
        }

        BmpFile::BmpFile(BinFile&& bin) : BmpFile(bin){
            bin.data = nullptr;
        }

        
        void BmpFile::AsBinFile(BinFile& bin){
            bin.data = reinterpret_cast<char *>(this->fileHeader);
            bin.size = this->size;
        }

        void BmpFile::AsBinFile(BmpFile&& bmp, BinFile& bin){
            bmp.AsBinFile(bin);
            bmp.fileHeader = nullptr;
            bmp.info = nullptr;
            bmp.rgb = nullptr;
        }
    }
}